package com.song.osclient.util;

import android.content.Context;
import android.preference.PreferenceManager;

import com.song.osclient.App;

/**
 * Created by Administrator on 2015/10/20.
 */
public class PreferenceHelper {
    public static void save(String key, Object value) {
        if (value instanceof String) {
            PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().putString(key, (String) value).apply();
        } else if (value instanceof Integer) {
            PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().putInt(key, (Integer) value).apply();
        } else if (value instanceof Boolean) {
            PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().putBoolean(key, (Boolean) value).apply();
        } else if (value instanceof Float) {
            PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().putFloat(key, (Float) value).apply();
        } else if (value instanceof Long) {
            PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().putLong(key, (Long) value).apply();
        }
    }

    public static Object get(String key, Object defaultValue) {
        if (defaultValue instanceof String) {
            return PreferenceManager.getDefaultSharedPreferences(App.getContext()).getString(key, (String) defaultValue);
        } else if (defaultValue instanceof Integer) {
            return PreferenceManager.getDefaultSharedPreferences(App.getContext()).getInt(key, (Integer) defaultValue);
        } else if (defaultValue instanceof Boolean) {
            return PreferenceManager.getDefaultSharedPreferences(App.getContext()).getBoolean(key, (Boolean) defaultValue);
        } else if (defaultValue instanceof Float) {
            return PreferenceManager.getDefaultSharedPreferences(App.getContext()).getFloat(key, (Float) defaultValue);
        } else if (defaultValue instanceof Long) {
            return PreferenceManager.getDefaultSharedPreferences(App.getContext()).getLong(key, (Long) defaultValue);
        }
        return null;
    }

    public static String getString(String key, String defaultValue) {
        return (String) get(key, defaultValue);
    }

    public static int getInt(String key, int defaultValue) {
        return (int) get(key, defaultValue);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return (boolean) get(key, defaultValue);
    }

    public static float getFloat(String key, int defaultValue) {
        return (float) get(key, defaultValue);
    }

    public static long getLong(String key, long defaultValue) {
        return (long) get(key, defaultValue);
    }

    public static void clearAllData() {
        PreferenceManager.getDefaultSharedPreferences(App.getContext()).edit().clear().apply();
    }

    public static boolean contains(String key) {
        return PreferenceManager.getDefaultSharedPreferences(App.getContext()).contains(key);
    }
}
